package com.ztgf.logistics.api.cms.deppon;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.FileUtils;
import com.ztgf.common.vo.UserVO;
import com.ztgf.file.models.bo.FileBO;
import com.ztgf.file.models.bo.FileInfoSaveBO;
import com.ztgf.logistics.models.service.deppon.IAsyncLogisticsOrderService;
import com.ztgf.logistics.models.service.deppon.ILogisticsBillOrderDepponSnapshotService;
import com.ztgf.sys.models.service.ISessionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.ztgf.logistics.models.service.deppon.ILogisticsBillPlatService;
import com.ztgf.logistics.models.bo.deppon.LogisticsBillPlatSaveBO;
import com.ztgf.logistics.models.bo.deppon.LogisticsBillPlatBO;
import com.ztgf.logistics.models.dto.deppon.LogisticsBillPlatDTO;
import org.springframework.web.multipart.MultipartFile;

/**
 * 物流平台账单表控制器
 *
 * @author mxli
 * @Date 2021-09-13 22:15:41
 */

@RestController
@RequestMapping("/cms/logisticsBillPlat")
@Slf4j
@Api(value = "LogisticsBillPlatController",tags = "物流平台账单表接口")
public class LogisticsBillPlatController {

    @DubboReference
    private ILogisticsBillPlatService logisticsBillPlatService;
    @DubboReference
    private ISessionService sessionService;
    @DubboReference
    private ILogisticsBillOrderDepponSnapshotService logisticsBillOrderDepponSnapshotService;
    @DubboReference
    private IAsyncLogisticsOrderService asyncOrderService;

    @ApiOperation(value = "添加物流平台账单表",notes = "添加物流平台账单表")
    @PostMapping(value = "/insert")
    public Result insert(LogisticsBillPlatSaveBO saveBO, @RequestParam("file") MultipartFile file) {

        if (null == file || file.isEmpty()) {
            return new Result<>(CodeEnum.FAIL_FILE_400000);
        }
        String originalFilename = file.getOriginalFilename();
        originalFilename = FileUtils.checkCompatible(originalFilename);
        String fileName = FileUtils.getFileName(originalFilename);
        String fileExtName = FileUtils.getFileExtName(originalFilename);
        byte[] input = new byte[0];
        try {
            input = file.getBytes();
        } catch (IOException e) {
            log.error("文件错误", e);
            return new Result(CodeEnum.FAIL_FILE_400001);
        }
        saveBO.setFileName(originalFilename);
        saveBO.setStartTime(saveBO.getStartTime());
        saveBO.setEndTime(saveBO.getEndTime());
        FileBO fileBO = new FileBO();
        fileBO.setFileName(fileName);
        fileBO.setFileExtName(fileExtName);
        fileBO.setInputs(input);
        fileBO.setFileSize(file.getSize());
        List<FileBO> list = new ArrayList<FileBO>();
        list.add(fileBO);
        FileInfoSaveBO fileSaveBO = new FileInfoSaveBO();
        fileSaveBO.setFileBOS(list);
        fileSaveBO.setPlatId(391630669681393666L);
        fileSaveBO.setBusinessType(10);//物流账单
        return logisticsBillPlatService.add(saveBO, input, fileSaveBO);

    }


    @PostMapping("/saveSure")
    @ApiOperation(value = "确认账单",notes = "确认账单")
     @ApiImplicitParams({
             @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
     })
     public Result saveSure(Long id){
        LogisticsBillPlatDTO dto = new LogisticsBillPlatDTO();
        UserVO userVO = sessionService.getUserVO();
        if (userVO!=null){
            dto.setCreateUserId(userVO.getId());
            dto.setCreateUserName(userVO.getName());
        }else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
        dto.setId(id);
        dto.setSureStatus(1);
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("id", id);
        wrapper.in("sure_status", "0,3");

        boolean flag = logisticsBillPlatService.update(dto, wrapper);
        if(flag){
            asyncOrderService.saveSure(id);
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
     }
    @ApiOperation(value = "删除物流平台账单表",notes = "删除物流平台账单表")
    @ApiImplicitParam(name = "id",value = "物流平台账单表id",required = true,paramType = "query",dataType = "Long")
    @PostMapping("/delete")
    public Result delete(@RequestParam("id") Long id){
        return logisticsBillPlatService.delete(id);
    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<LogisticsBillPlatDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        LogisticsBillPlatDTO dto = logisticsBillPlatService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "物流平台账单表列表",notes = "物流平台账单表列表")
    @PostMapping("/queryPageList")
    public Result<Page<LogisticsBillPlatDTO>> queryPageList(LogisticsBillPlatBO bo){
        Wrapper<LogisticsBillPlatDTO> wrapper = new EntityWrapper<>();

        if (bo.getStatus() != null){
            wrapper.eq("status", bo.getStatus());
        }
        if (bo.getStartDate() != null){
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null){
            wrapper.le("create_time", bo.getEndDate());
        }
        wrapper.orderBy("start_time", false);
        Page<LogisticsBillPlatDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = logisticsBillPlatService.selectPage(page, wrapper);

        return new Result<>(page);
    }
}
